<template>
  <div class="login-box">
    <el-form ref="form" :model="form" :rules="rules">
      <img src="@/assets/common/login-logo.png" alt="">
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          show-password
          prefix-icon="el-icon-lock"
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="submit">登录</el-button>
      </el-form-item>
      <div class="tip">还未注册登录账户号？点我注册</div>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        mobile: '',
        password: ''
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确手机号',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        console.log('验证结果', result)
        // 等待登录成功跳转首页
        await this.$store.dispatch('user/toLogin', this.form)
        this.$message.success('登录成功')
        const redirect = this.$route.query.redirect || '/'
        this.$router.push(redirect)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-box {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../../assets/common/login.jpg');
  background-size: cover;
  img {
    width: 100%;
    margin-bottom: 20px;
  }
  .el-button {
    width: 100%;
    background-color: rgb(79, 152, 206);
    border: 1px solid rgb(79, 152, 206);
    color: #fff;
  }
  .tip {
    text-align: center;
    margin-top: 25px;
    color: #fff;
    font-size: 14px;
  }
}
</style>
